package com.lill.org.api.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.lill.common.component.util.R;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.EmployeeModel;
import com.lill.org.service.ICompanyArchitectureService;
import com.lill.org.service.IEmployeeService;
import com.lill.support.annotation.AuthTokenOnce;
import com.lill.system.model.SysUserLoginAppEntity;

@Api(value="api/orgAndEmployee", tags="用户结构")
@RestController
@RequestMapping("api/orgAndEmployee")
public class OrgAndEmployeeAction {
	
	@Autowired
    private ICompanyArchitectureService architectureService;
	@Autowired
	private IEmployeeService employeeService;
	
	@ApiOperation(value="获取节点信息、机构、人员",notes="获取节点信息、机构、人员",httpMethod="GET")
	@AuthTokenOnce
	@GetMapping("/list")
	public R orgList(@ApiIgnore SysUserLoginAppEntity appUser,@RequestParam(required=false) String nodeId) {
		Map<String, Object> dataMap=new HashMap<String, Object>();
		
		if(StringUtils.isEmpty(nodeId)){
			nodeId=appUser.getDepartId();
		}
		
		Map<String, Object> queryMap=new HashMap<String, Object>();
		queryMap.put("parentId", nodeId);
		queryMap.put("parentList", appUser.getCompanyId());
		List<CompanyArchitectureModel> architList = architectureService.queryList(queryMap);
		
		queryMap=new HashMap<String, Object>();
		queryMap.put("departId", nodeId);
		List<EmployeeModel> employeeList = employeeService.queryList(queryMap);
		
		dataMap.put("org", architList);
		dataMap.put("emp", employeeList);
		
		queryMap=new HashMap<String, Object>();
		queryMap.put("nodeId", nodeId);
		List<CompanyArchitectureModel> parentsList=new ArrayList<CompanyArchitectureModel>();
		String parent="nodeId";
		List<CompanyArchitectureModel> nodeList=null;
		while (!"0".equals(parent)) {
			nodeList = architectureService.queryList(queryMap);
			if(CollectionUtils.isNotEmpty(nodeList)){
				parentsList.add(0, nodeList.get(0));
				parent=nodeList.get(0).getParentId();
				queryMap.put("nodeId", parent);
			}else{
				parent="0";
			}
		}
		dataMap.put("par", parentsList);
		return R.ok().put("data",dataMap);
	}
	
	@ApiOperation(value="查询获取节人员",notes="查询获取节人员",httpMethod="GET")
	@AuthTokenOnce
	@GetMapping("/emplist")
	public R employeeList(@ApiIgnore SysUserLoginAppEntity appUser,@RequestParam String search) {
		Map<String, Object> queryMap=new HashMap<String, Object>();
		queryMap.put("companyId", appUser.getCompanyId());
		queryMap.put("search", search);
		List<EmployeeModel> employeeList = employeeService.queryList(queryMap);
		return R.ok().put("data",employeeList);
	}
}
